package com.dcm.item.service.impl;


import com.dcm.common.constant.Constants;
import com.dcm.common.exception.MyException;
import com.dcm.item.entity.CategoryEntity;
import com.dcm.item.mapper.CategoryMapper;
import com.dcm.item.service.CategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author:zhouyuhan
 * 时间 2021/3/22 11:12
 */

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<CategoryEntity> findAll() {
        return categoryMapper.findAll();
    }

    /**
     * 添加一条数据
     * @param tbCategory
     * @return
     */
    @Override
    public void add(CategoryEntity tbCategory) throws MyException {

        checkItem(tbCategory);
        categoryMapper.add(tbCategory);

    }



    /**
     * 修改整条数据
     * @param tbCategory
     * @return
     */
    @Override
    public void update(CategoryEntity tbCategory) throws MyException {

       checkItem(tbCategory);
        categoryMapper.update(tbCategory);
    }

    /**
     * 通过id删除
     * @param id
     * @return
     */
    @Override
    public void delete(Long id) throws MyException {
        if (id==null){
            throw new MyException(Constants.CategoryErrorMsg.CATEGORY_ID_CANNOT_NULL);
        }
         categoryMapper.delete(id);
    }

    /**
     * 模糊姓名和状态
     * @param status
     * @param name
     * @return
     */
    @Override
    public List<CategoryEntity> findByStatusAndName(Integer status, String name) throws MyException {
        if (status==null){
            throw new MyException(Constants.CategoryErrorMsg.CATEGORY_STATUS_CANNOT_NULL);
        }
        if (StringUtils.isEmpty(name)){
            throw new MyException(Constants.CategoryErrorMsg.CATEGORY_NAME_CANNOT_NULL);
        }
        return categoryMapper.findByStatusAndName(status,name);
    }

    /**
     * 添加子目录
     * @param category
     * @return
     */
    @Override
    public void addSecondCategory(CategoryEntity category) throws MyException {

         checkItem(category);
         categoryMapper.add(category);
    }



    /**
     * 通过id修改状态
     * @param id
     * @param status
     * @return
     */
     @Override
     public void updateStatus(Long id, Integer status) throws MyException {
         if (id==null){
             throw new MyException(Constants.CategoryErrorMsg.CATEGORY_ID_CANNOT_NULL);
         }
         if (status==null){
             throw new MyException(Constants.CategoryErrorMsg.CATEGORY_STATUS_CANNOT_NULL);
         }
         categoryMapper.updateStatus(id,status);
     }

    @Override
    public PageInfo<CategoryEntity> findPage(Integer currentPage, Integer pageSize) {
        if (currentPage == null || pageSize == null) {
            throw new MyException(Constants.ResponseMsg.PAGE_PARAMS_NOT_NULL);
        }

        PageHelper.startPage(currentPage, pageSize);
        List<CategoryEntity> list = categoryMapper.findAll();
        //用PageInfo对结果进行包装
        PageInfo<CategoryEntity> page = new PageInfo<>(list);

        return page;
    }

    public static void checkItem(CategoryEntity category){
         if (StringUtils.isEmpty(category.getName())){
             throw new MyException(Constants.CategoryErrorMsg.CATEGORY_NAME_CANNOT_NULL);
         }
         if (StringUtils.isEmpty(category.getImages())){
            throw new MyException(Constants.CategoryErrorMsg.CATEGORY_IMAGES_CANNOT_NULL);
         }
         if (StringUtils.isEmpty(category.getType())){
            throw new MyException(Constants.CategoryErrorMsg.CATEGORY_TYPE_CANNOT_NULL);
         }
         if (category.getSsort()==null){
             throw new MyException(Constants.CategoryErrorMsg.CATEGORY_SORT_CANNOT_NULL);
         }
         if (category.getPid()==null){
            throw new MyException(Constants.CategoryErrorMsg.CATEGORY_PID_CANNOT_NULL);
         }



    }
}
